home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / object / orb__define.pro < prev    next >
Text File  |  1997-07-08  |  9KB  |  330 lines

  1. ; $Id: orb__define.pro,v 1.1 1997/04/18 00:43:32 griz Exp $
  2. ;
  3. ; Copyright (c) 1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;    ORB
  8. ;
  9. ; PURPOSE:
  10. ;    This object serves as a graphical representation of an orb,
  11. ;    (or sphere), which subclasses from the IDLgrModel class.
  12. ;
  13. ; CATEGORY:
  14. ;    Object graphics.
  15. ;
  16. ; CALLING SEQUENCE:
  17. ;    To initially create:
  18. ;               oOrb = OBJ_NEW('orb') 
  19. ;
  20. ;    To retrieve a property value:
  21. ;        oOrb->GetProperty
  22. ;
  23. ;    To set a property value:
  24. ;        oOrb->SetProperty
  25. ;
  26. ;    To print to the standard output stream the current properties of 
  27. ;    the orb:
  28. ;        oOrb->Print
  29. ;
  30. ;    To destroy:
  31. ;        OBJ_DESTROY, oOrb
  32. ;
  33. ; KEYWORD PARAMETERS:
  34. ;   ORB::INIT:
  35. ;    <Note that keywords accepted by IDLgrModel::Init and/or
  36. ;     IDLgrPolygon::Init are also accepted here.>
  37. ;    POS:    A three-element vector, [x,y,z], specifying the position
  38. ;               of the center of the orb, measured in data units . 
  39. ;        Defaults to [0,0,0].
  40. ;    RADIUS: A floating point number representing the radius of the
  41. ;               orb (measured in data units).  The default is 1.0.
  42. ;    DENSITY: A floating point number representing the density at which
  43. ;               the vertices should be generated along the surface of the
  44. ;               orb.  The default is 1.0.
  45. ;    TEX_COORDS: Set this keyword to a nonzero value if texture map
  46. ;               coordinates are to be generated for the orb.
  47. ;
  48. ;   ORB::GETPROPERTY:
  49. ;    POS:    Set this keyword to a named variable that upon return will
  50. ;        contain a three-element vector, [x,y,z], specifying the 
  51. ;        position of the center of the orb, measured in data units . 
  52. ;    RADIUS: Set this keyword to a named variable that upon return will
  53. ;        contain a floating point number representing the radius of the
  54. ;               orb (measured in data units).
  55. ;    DENSITY: Set this keyword to a named variable that upon return will
  56. ;        contain a floating point number representing the density at 
  57. ;        which the vertices are generated along the surface of the
  58. ;               orb.
  59. ;
  60. ;   ORB::SETPROPERTY:
  61. ;    <Note that keywords accepted by IDLgrModel::SetProperty and/or
  62. ;     IDLgrPolygon::SetProperty are also accepted here.>
  63. ;    POS:    A three-element vector, [x,y,z], specifying the position
  64. ;               of the center of the orb. Defaults to [0,0,0].
  65. ;    RADIUS: A floating point number representing the radius of the
  66. ;               orb (measured in data units).  The default is 1.0.
  67. ;    DENSITY: A floating point number representing the density at which
  68. ;               the vertices should be generated along the surface of the
  69. ;               orb.  The default is 1.0.
  70. ;
  71. ; EXAMPLE:
  72. ;    Create an orb centered at the origin with a radius of 0.5:
  73. ;        oOrb = OBJ_NEW('Orb', POS=[0,0,0], RADIUS=0.5) 
  74. ;
  75. ; MODIFICATION HISTORY:
  76. ;     Written by:    RF, September 1996.
  77. ;-
  78.  
  79. ;----------------------------------------------------------------------------
  80. ; ORB::INIT
  81. ;
  82. ; Purpose:
  83. ;  Initializes an orb object.
  84. ;
  85. ;  This function returns a 1 if initialization is successful, or 0 otherwise.
  86. ;
  87. FUNCTION Orb::Init, POS=pos, RADIUS=radius, DENSITY=density, $
  88.                        TEX_COORDS=tex_coords, _EXTRA=e
  89.  
  90.     IF (self->IDLgrModel::Init(_EXTRA=e) NE 1) THEN RETURN, 0
  91.  
  92.     self.pos = [0.0,0.0,0.0]
  93.     self.radius = 1.0
  94.     self.density = 1.0
  95.  
  96.     IF (N_ELEMENTS(pos) EQ 3) THEN $
  97.         self.pos = pos
  98.  
  99.     IF (N_ELEMENTS(radius) EQ 1) THEN $
  100.         self.radius = radius
  101.  
  102.     IF (N_ELEMENTS(density) EQ 1) THEN $
  103.         self.density = density
  104.  
  105.     IF (N_ELEMENTS(tex_coords) EQ 1) THEN $
  106.         self.texture = tex_coords
  107.  
  108.     ; Initialize the polygon object that will be used to represent
  109.     ; the orb.
  110.     self.oPoly = OBJ_NEW('IDLgrPolygon', SHADING=1, /REJECT, _EXTRA=e)
  111.  
  112.     self->Add,self.oPoly
  113.  
  114.     ; Build the polygon vertices and connectivity based on property settings.
  115.     self->BuildPoly
  116.  
  117.     RETURN, 1
  118. END
  119.  
  120. ;----------------------------------------------------------------------------
  121. ; ORB::CLEANUP
  122. ;
  123. ; Purpose:
  124. ;  Cleans up all memory associated with the orb.
  125. ;
  126. PRO orb::Cleanup
  127.  
  128.     ; Cleanup the polygon object used to represent the orb.
  129.     OBJ_DESTROY, self.oPoly
  130.  
  131.     ; Cleanup the superclass.
  132.     self->IDLgrModel::Cleanup
  133. END
  134.  
  135. ;----------------------------------------------------------------------------
  136. ; ORB::SETPROPERTY
  137. ;
  138. ; Purpose:
  139. ;  Sets the value of properties associated with the orb object.
  140. ;
  141. PRO orb::SetProperty, POS=pos, RADIUS=radius, DENSITY=density, _EXTRA=e
  142.  
  143.     ; Pass along extraneous keywords to the superclass and/or to the
  144.     ; polygon used to represent the orb.
  145.     self->IDLgrModel::SetProperty, _EXTRA=e
  146.     self.oPoly->SetProperty, _EXTRA=e
  147.  
  148.     IF (N_ELEMENTS(pos) EQ 3) THEN $
  149.         self.pos = pos
  150.  
  151.     IF (N_ELEMENTS(radius) EQ 1) THEN $
  152.         self.radius = radius
  153.  
  154.     IF (N_ELEMENTS(density) EQ 1) THEN $
  155.         self.density = density
  156.  
  157.     ; Rebuild the polygon according to keyword settings.
  158.     self->BuildPoly
  159. END
  160.  
  161. ;----------------------------------------------------------------------------
  162. ; ORB::GETPROPERTY
  163. ;
  164. ; Purpose:
  165. ;  Retrieves the value of properties associated with the orb object.
  166. ;
  167. PRO orb::GetProperty, POS=pos, RADIUS=radius, DENSITY=density,$
  168.                          POBJ=pobj, _EXTRA=e
  169.  
  170.     self->IDLgrModel::GetProperty, _EXTRA=e
  171.     self.oPoly->GetProperty, _EXTRA=e
  172.  
  173.     pos = self.pos
  174.     radius = self.radius 
  175.     density = self.density 
  176.     pobj = self.oPoly
  177. END
  178.  
  179. PRO orb::Print
  180.     PRINT, self.pos
  181.     PRINT, self.radius
  182.     PRINT, self.density
  183. END
  184.  
  185. ;----------------------------------------------------------------------------
  186. ; ORB::BUILDPOLY
  187. ;
  188. ; Purpose:
  189. ;  Sets the vertex and connectivity arrays for the polygon used to
  190. ;  represent the orb.
  191. ;
  192. PRO orb::BuildPoly
  193.     ; Build the orb.
  194.  
  195.     ; Number of rows and columns of vertices is based upon the density
  196.     ; property.
  197.     nrows = LONG(20.0*self.density)
  198.     ncols = LONG(20.0*self.density)
  199.     IF (nrows LT 2) THEN nrows = 2
  200.     IF (ncols LT 2) THEN ncols = 2
  201.  
  202.     ; Create the vertex list and the connectivity array.
  203.     nverts = nrows*ncols + 2
  204.     nconn = (ncols*(nrows-1)*5) + (2*ncols*4)
  205.     conn = LONARR(ncols*(nrows-1)*5 + 2*ncols*4)
  206.     verts = FLTARR(3, nverts)
  207.     IF (self.texture NE 0) THEN $
  208.         tex = FLTARR(2,nverts)
  209.  
  210.     ; Fill in the vertices.
  211.     i = 0L
  212.     j = 0L
  213.     k = 0L
  214.     tzinc = (!PI)/FLOAT(nrows+1)
  215.     tz = (!PI/2.0) - tzinc 
  216.     FOR k=0,(nrows-1) DO BEGIN
  217.         z = SIN(tz)*self.radius
  218.         r = COS(tz)*self.radius
  219.         t = 0
  220.         IF (self.texture NE 0) THEN BEGIN
  221.             tinc = (2.*!PI)/FLOAT(ncols-1)
  222.         ENDIF ELSE BEGIN
  223.             tinc = (2.*!PI)/FLOAT(ncols)
  224.         ENDELSE
  225.         FOR j=0,(ncols-1) DO BEGIN
  226.             verts[0,i] = r*COS(t) + self.pos[0]
  227.             verts[1,i] = r*SIN(t) + self.pos[1]
  228.             verts[2,i] = z + self.pos[2]
  229.  
  230.             IF (self.texture NE 0) THEN BEGIN
  231.                 tex[0,i] = t/(2.*!PI)
  232.                 tex[1,i] = (tz+(!PI/2.0))/(!PI)
  233.             ENDIF
  234.  
  235.             t = t + tinc
  236.             i = i + 1L
  237.         ENDFOR
  238.         tz = tz - tzinc
  239.     ENDFOR
  240.     top = i
  241.     verts[0,i] = self.pos[0]
  242.     verts[1,i] = self.pos[1]
  243.     verts[2,i] = self.radius*1.0 + self.pos[2]
  244.     i = i + 1L
  245.     bot = i
  246.     verts[0,i] = self.pos[0]
  247.     verts[1,i] = self.pos[1]
  248.     verts[2,i] = self.radius*(-1.0) + self.pos[2]
  249.  
  250.     IF (self.texture NE 0) THEN BEGIN
  251.         tex[0,i] = 0.5
  252.         tex[1,i] = 0.0
  253.         tex[0,i-1] = 0.5
  254.         tex[1,i-1] = 1.0
  255.     ENDIF
  256.  
  257.     ; Fill in the connectivity array.
  258.     i = 0
  259.     FOR k=0,(nrows-2) DO BEGIN
  260.         FOR j=0,(ncols-1) DO BEGIN
  261.             conn[i] = 4
  262.  
  263.             conn[i+4] = k*ncols + j
  264.  
  265.             w = k*ncols + j + 1L
  266.             IF (j EQ (ncols-1)) THEN w = k*ncols
  267.             conn[i+3] = w
  268.  
  269.             w = k*ncols + j + 1L + ncols
  270.             IF (j EQ (ncols-1)) THEN w = k*ncols + ncols
  271.             conn[i+2] = w
  272.  
  273.             conn[i+1] = k*ncols + j + ncols
  274.  
  275.             i = i + 5L
  276.             IF ((self.texture NE 0) AND (j EQ (ncols-1))) THEN $
  277.                 i = i - 5L
  278.         ENDFOR
  279.     ENDFOR
  280.     FOR j=0,(ncols-1) DO BEGIN
  281.         conn[i] = 3
  282.         conn[i+3] = top
  283.         conn[i+2] = j+1L
  284.         IF (j EQ (ncols-1)) THEN conn[i+2] = 0
  285.         conn[i+1] = j
  286.         i = i + 4L
  287.         IF ((self.texture NE 0) AND (j EQ (ncols-1))) THEN $
  288.             i = i - 4L
  289.     ENDFOR
  290.     FOR j=0,(ncols-1) DO BEGIN
  291.         conn[i] = 3
  292.         conn[i+3] = bot
  293.         conn[i+2] = j+(nrows-1L)*ncols
  294.         conn[i+1] = j+(nrows-1L)*ncols+1L
  295.         IF (j EQ (ncols-1)) THEN conn[i+1] = (nrows-1L)*ncols
  296.         i = i + 4L
  297.         IF ((self.texture NE 0) AND (j EQ (ncols-1))) THEN $
  298.             i = i - 4L
  299.     ENDFOR
  300.  
  301.     self.oPoly->SetProperty, DATA=verts, POLYGONS=conn
  302.  
  303.     IF (self.texture NE 0) THEN $
  304.         self.oPoly->SetProperty, TEXTURE_COORD=tex
  305. END
  306.  
  307. ;----------------------------------------------------------------------------
  308. ; ORB__DEFINE
  309. ;
  310. ; Purpose:
  311. ;  Defines the object structure for an orb object.
  312. ;
  313. PRO orb__define
  314.     struct = { orb, $
  315.                INHERITS IDLgrModel, $
  316.                pos: [0.0,0.0,0.0], $
  317.                radius: 1.0, $
  318.                density: 1.0, $
  319.                texture: 0, $
  320.                oPoly: OBJ_NEW() $
  321.              }
  322. END
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.